layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 可好提总单明细管理
     */
    var KhOrderOutItem = {
        tableId: "khOrderOutItemTable"
    };

    /**
     * 初始化表格的列
     */
    KhOrderOutItem.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'orderOutNumber', sort: true, title: '出库订单'},
            {field: 'goodsId', sort: true, title: '商品id'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'unit', sort: true, title: '单位'},
            {field: 'rate', sort: true, title: '换算率'},
            {field: 'psNum', sort: true, title: '数量'},
            {field: 'dismantlingParts', sort: true, title: '是否拆零'},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'jibenNum', sort: true, title: '基本数量'},
            {field: 'goodsXcode69', sort: true, title: '商品码2'},
            {field: 'goodsHcode69', sort: true, title: '商品码3'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    KhOrderOutItem.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(KhOrderOutItem.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     KhOrderOutItem.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     KhOrderOutItem.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     KhOrderOutItem.exportExcel();
                 });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    KhOrderOutItem.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/khOrderOutItem/add';
    };

    /**
     * 导出excel按钮
     */
    KhOrderOutItem.exportExcel = function () {
        var checkRows = table.checkStatus(KhOrderOutItem.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    KhOrderOutItem.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/khOrderOutItem/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    KhOrderOutItem.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/khOrderOutItem/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(KhOrderOutItem.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + KhOrderOutItem.tableId,
        url: Feng.ctxPath + '/khOrderOutItem/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: KhOrderOutItem.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        KhOrderOutItem.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        KhOrderOutItem.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        KhOrderOutItem.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + KhOrderOutItem.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            KhOrderOutItem.openEditDlg(data);
        } else if (layEvent === 'delete') {
            KhOrderOutItem.onDeleteItem(data);
        }
    });
});
